﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Linq;
using XDCCManager.EventHandlers;
using XDCCManager.Model.Servers;

namespace XDCCManager.Popups
{
	/// <summary>
	/// Interaction logic for ServerEditor.xaml
	/// </summary>
	public partial class ServerEditor : UserControl
	{
		private IDictionary<UploadServiceKind, UploadService> savedServices;

		public ServerEditor()
		{
			savedServices = new Dictionary<UploadServiceKind, UploadService>();
			InitializeComponent();
		}

		private void Ok_Click(object sender, RoutedEventArgs e)
		{
			if (Ok != null)
				Ok(this, new ServerEventArgs(DataContext as Server));
		}

		private void Cancel_Click(object sender, RoutedEventArgs e)
		{
			if (Cancel != null)
				Cancel(this, new EventArgs());
		}

		public event EventHandler<ServerEventArgs> Ok;
		public event EventHandler Cancel;

		public bool ShowRemove
		{
			get { return (bool)GetValue(ShowRemoveProperty); }
			set { SetValue(ShowRemoveProperty, value); }
		}

		// Using a DependencyProperty as the backing store for ShowRemove.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty ShowRemoveProperty =
			DependencyProperty.Register("ShowRemove", typeof(bool), typeof(ServerEditor), new UIPropertyMetadata(null));

		private void Add_Click(object sender, RoutedEventArgs e)
		{
			(DataContext as Server).Addresses.Add(new ServerAddress());
		}

		private void Remove_Click(object sender, RoutedEventArgs e)
		{
			ServerAddress address = FindServerAddressItem(e.Source as DependencyObject);
			if (address != null)
				(DataContext as Server).Addresses.Remove(address);
		}

		private ServerAddress FindServerAddressItem(DependencyObject element)
		{
			while ((element != null) && (!(element is ListViewItem)))
			{
				element = VisualTreeHelper.GetParent(element);
			}
			if (element == null)
				return null;
			return (element as FrameworkElement).DataContext as ServerAddress;
		}

		private void ItemRemove_Click(object sender, RoutedEventArgs e)
		{
			if (Remove != null)
				Remove(this, new EventArgs());
		}

		public event EventHandler Remove;

		private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
		{
			if (DataContext != null)
				(DataContext as Server).Password = password.Password;
		}

		private void userControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			if (e == null)
				return;
			if (e.OldValue == e.NewValue)
				return;
			if ((e.OldValue != null) && (e.OldValue is Server))
			{
				(e.OldValue as Server).PropertyChanged -= Server_PropertyChanged;
			}
			savedServices.Clear();
			if ((e.NewValue != null) && (e.NewValue is Server))
			{
				if (password != null)
				{
					password.Password = (e.NewValue as Server).Password;
				}
				(e.NewValue as Server).PropertyChanged += Server_PropertyChanged;
				savedServices[(e.NewValue as Server).UploadServiceKind] = (e.NewValue as Server).UploadService;
			}
		}

		void Server_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			Server server = DataContext as Server;
			if (server == null)
				return;
			switch (e.PropertyName)
			{
				case "UploadServiceKind":
					if (!savedServices.ContainsKey(server.UploadServiceKind))
					{
						object[] attributes = server.UploadServiceKind.GetType().GetField(server.UploadServiceKind.ToString()).GetCustomAttributes(typeof(UploadServiceTypeAttribute), false);
						if (attributes.Length == 0)
						{
							savedServices[server.UploadServiceKind] = null;
						}
						else
						{
							savedServices[server.UploadServiceKind] = (attributes[0] as UploadServiceTypeAttribute).Type.GetConstructor(Type.EmptyTypes).Invoke(null) as UploadService;
						}
					}
					server.UploadService = savedServices[server.UploadServiceKind];
					break;
			}
		}
	}
}
